<?php

class User extends DBItem {

	public $Username;
	public $Password;
	public $Email;
	public $Class;
	public $idUserGroup;
	public $IsHidden = false;
	public $IsBanned = false;
	public $Comments;
	public $idCreatedBy;
	public $DateCreated;
	public $idModifiedBy;
	public $DateModified;

	public static function FillMetaClass(MetaClass $c){
		$c->SetDBTableName('oxy_users');
		$c->SetOrderBy('XUsername');
		$c->SetAbstractDBFieldName('XClass');
		$c->Username = MetaField::String()->WithAlias('XUsername')->WithLabel('Username');
		$c->Password = MetaField::String()->WithAlias('XPassword')->WithLabel('Password');
		$c->Email = MetaField::String()->WithLabel('Email');
		$c->Class = MetaField::String()->WithAlias('XClass');
		$c->idUserGroup = MetaField::ID();
		$c->IsHidden = MetaField::Boolean();
		$c->IsBanned = MetaField::Boolean()->WithLabel('AccountLocked');
		$c->Comments = MetaField::String()->WithLabel('Comments');
		$c->idCreatedBy = MetaField::ID();
		$c->DateCreated = MetaField::DateTime();
		$c->idModifiedBy = MetaField::ID();
		$c->DateModified = MetaField::DateTime();
	}


	protected function OnBeforeSave(){
		$this->idModifiedBy = User::GetCurrent()->id;
		$this->DateModified = new DateTime();
		if ($this->idCreatedBy == null){
			$this->idCreatedBy = $this->idModifiedBy;
			$this->DateCreated = $this->DateModified;
		}
		$this->Class = get_called_class();
	}



	public function GetUserGroup(){ return UserGroup::Retrieve($this->idUserGroup);}

	public function IsVisitor(){ return $this->GetUserGroup()->IsDescendantOf(UserGroup::idVisitors); }
	public function IsAdministrator(){ return $this->GetUserGroup()->IsDescendantOf(UserGroup::idAdministrators); }
	public function IsDeveloper(){ return $this->GetUserGroup()->IsDescendantOf(UserGroup::idDevelopers); }


	public function HasDroit($objet,$verbe){ return $this->GetUserGroup()->HasDroit($objet,$verbe); }









	const idVisitor = 1;
	const idUser = 2;
	const idAdministrator = 3;
	const idDeveloper = 4;
	public static function GetVisitor(){ return User::Retrieve(self::idVisitor); }




	private static $current = null;
	public static function IsLoggedIn(){
		if (!Database::IsConnected()) return false;
		return !User::GetCurrent()->IsEqual(User::idVisitor);
	}
	public static function GetCurrent(){
		if (!Database::IsConnected()) return null;
		if ( is_null(self::$current) ) {
			self::$current = User::Retrieve(Scope::$SESSION['User::idCurrent']);
			if (is_null(self::$current))
				self::SetCurrent(User::GetVisitor());
		}
		return self::$current;
	}
	private static function SetCurrent($user){
		self::$current = $user;
		Scope::$SESSION['User::idCurrent'] = is_null($user) ? null : $user->id;
	}

	public static function Disconnect(){ self::SetCurrent(null); }
	public static function Logoff(){ self::SetCurrent(User::GetVisitor()); }
	public static function Login($username,$password){
		$u = User::RetrieveByUsername($username);
		if ($u == null)
			throw new ApplicationException(Lemma::ErrorInvalidUsername());
		if ($u->IsBanned)
			throw new ApplicationException(Lemma::ErrorAccountBanned());
		if ($u->Password != Oxygen::Hash($password))
			throw new ApplicationException(Lemma::ErrorInvalidPassword());
		self::SetCurrent($u);
	}



	public static function RetrieveByUsername($username){	foreach (self::Select('XUsername='.new Sql($username)) as $u) return $u; return null; }
}
?>
